package org.example.new_hbps_backend_reconstruction.controller.patient;

/**
 * @author 丁利军
 * @date 2025年10月15日 21:38
 * @description 步数记录管理
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.new_hbps_backend_reconstruction.entity.StepCountLogsEntity;
import org.example.new_hbps_backend_reconstruction.service.StepCountLogsService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/patient/step-count")
@Tag(name = "步数记录管理", description = "提供患者步数数据查询接口，包括查询最新数据、分页查询、按日期查询、查询所有记录")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class StepCountLogsController {

    private final StepCountLogsService stepCountLogsService;

    // 1. 查找最新一条
    @Operation(
            summary = "查询最新步数记录",
            description = "根据 patientUid 查询该患者最新一条步数记录"
    )
    @Parameter(
            name = "patientUid",
            description = "患者 UID",
            required = true
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = StepCountLogsEntity.class))),
            @ApiResponse(responseCode = "404", description = "未找到对应记录"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/latest/{patientUid}")
    public ResponseEntity<StepCountLogsEntity> getLatest(@PathVariable Long patientUid) {
        log.info("查询 patientUid={} 的最新步数记录", patientUid);
        return ResponseEntity.ok(stepCountLogsService.getLatestByPatientUid(patientUid));
    }

    // 2. 分页查找
    @Operation(
            summary = "分页查询步数记录",
            description = "根据 patientUid 分页查询步数记录"
    )
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "pageNum", description = "页码（默认1）", required = false),
            @Parameter(name = "pageSize", description = "每页条数（默认10）", required = false)
    })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/page/{patientUid}")
    public ResponseEntity<IPage<StepCountLogsEntity>> getPage(
            @PathVariable Long patientUid,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        log.info("分页查询 patientUid={} 的步数记录, pageNum={}, pageSize={}", patientUid, pageNum, pageSize);
        return ResponseEntity.ok(stepCountLogsService.getPageByPatientUid(patientUid, pageNum, pageSize));
    }

    // 3. 根据日期查找
    @Operation(
            summary = "按日期查询步数记录",
            description = "根据 patientUid 和日期查询对应的步数记录"
    )
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "date", description = "日期（格式：yyyy-MM-dd）", required = true)
    })
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "参数格式错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/by-date/{patientUid}")
    public ResponseEntity<List<StepCountLogsEntity>> getByDate(
            @PathVariable Long patientUid,
            @RequestParam String date) {
        log.info("按日期查询 patientUid={} 的步数记录, date={}", patientUid, date);
        LocalDate localDate = LocalDate.parse(date);
        return ResponseEntity.ok(stepCountLogsService.getByPatientUidAndDate(patientUid, localDate));
    }

    // 4. 查找所有
    @Operation(
            summary = "查询所有步数记录",
            description = "根据 patientUid 查询该患者的所有步数记录"
    )
    @Parameter(
            name = "patientUid",
            description = "患者 UID",
            required = true
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/all/{patientUid}")
    public ResponseEntity<List<StepCountLogsEntity>> getAll(@PathVariable Long patientUid) {
        log.info("查询 patientUid={} 的所有步数记录", patientUid);
        return ResponseEntity.ok(stepCountLogsService.getAllByPatientUid(patientUid));
    }
}